/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/3/5 22:15
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.dubbo.netty;

import com.hyacinth.dubbo.io.netty.client.handler.FirstClientHandler;
import com.hyacinth.dubbo.io.netty.codec.PacketCodecHandler;
import com.hyacinth.dubbo.io.netty.codec.Spliter;
import com.hyacinth.dubbo.io.netty.monitor.IMIdleStateHandler;
import com.hyacinth.dubbo.io.netty.server.handler.AuthHandler;
import com.hyacinth.dubbo.io.netty.server.handler.HeartBeatRequestHandler;
import com.hyacinth.dubbo.io.netty.server.handler.IMHandler;
import com.hyacinth.dubbo.io.netty.server.handler.LoginRequestHandler;
import com.hyacinth.dubbo.io.netty.server.handler.test.FirstServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;

/**
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class Server {

    private static final int PORT = 8080;

    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap
                .group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) {
                        ch.pipeline().addLast(new FirstServerHandler());
                    }
                });
        // 绑定端口
        bind(serverBootstrap, PORT);
    }


    /**
     * 递增绑定端口
     */
    private static void bind(final ServerBootstrap serverBootstrap, final int port) {
        serverBootstrap.bind(port).addListener(future -> {
            if (future.isSuccess()) {
                System.out.println(new Date() + ": 端口[" + port + "]绑定成功!");
            } else {
                System.err.println("端口[" + port + "]绑定失败!");
            }
        });
    }
}
